package com.qf;

import com.qf.mapper.UserMapper;
import com.qf.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 金园
 * @create 2022/4/27
 * @Description
 * @className testMyBatisCRUD
 * @packageName com.qf
 */
public class testMyBatisCRUD {

    SqlSessionFactory SessionFactory;
    SqlSession session;
    @Before //在执行test方法之前执行
    public void before()throws Exception{
        //获取核心配置文件的流对象
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        //创建SqlSessionFactory对象的过构建器对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        //构建SqlSessionFactory对象
         SessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }

    @After //在执行test方法之后执行
    public void after(){
        //释放资源
        session.close();
    }


    @Test
    public void testQueryAll()throws Exception{

        //获取SqlSession对象 (包含了connection，封装了更多的方法)
        session = SessionFactory.openSession();
        //获取接口的动态代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //执行方法
        List<User> users = mapper.queryAll();
        for (User user : users) {
            System.out.println(user);
        }



    }
    @Test
    public void testQueryById()throws Exception{

        //获取SqlSession对象 (包含了connection，封装了更多的方法)
        session = SessionFactory.openSession();
        //获取接口的动态代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //执行方法
        User user = mapper.queryById(1);
        System.out.println(user);
    }
    @Test
    public void testTotalCount()throws Exception{

        //获取SqlSession对象 (包含了connection，封装了更多的方法)
        session = SessionFactory.openSession();
        //获取接口的动态代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //执行方法
        Long l = mapper.totalCount();
        System.out.println(l);
    }
    @Test
    public void testQueryLike()throws Exception{

        //获取SqlSession对象 (包含了connection，封装了更多的方法)
        session = SessionFactory.openSession();
        //获取接口的动态代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //执行方法
        List<User> users = mapper.queryLike("三");
        System.out.println(users);
    }

    @Test
    public void testInsert()throws Exception{
        User user = new User();
        user.setName("老六");
        user.setPassword("123");
        user.setSex("男");
        user.setBirthday(new Date());
        user.setRegistTime(new Date());
        //获取SqlSession对象 (包含了connection，封装了更多的方法)
        session = SessionFactory.openSession();
        //获取接口的动态代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.insert(user);
    }

    @Test
    public void testupdate()throws Exception{
        User user = new User();
        user.setId(4);
        user.setName("老六");
        user.setPassword("123");
        user.setSex("女");
        user.setBirthday(new Date());
        user.setRegistTime(new Date());
        //获取SqlSession对象 (包含了connection，封装了更多的方法)
        session = SessionFactory.openSession();
        //获取接口的动态代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.update(user);
    }
    
    @Test
    public void testDelete()throws Exception{
        //获取SqlSession对象 (包含了connection，封装了更多的方法)
        session = SessionFactory.openSession();
        //获取接口的动态代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.delete(4);
    }

    @Test
    public void testQueryByCondition(){
        //获取SqlSession对象 (包含了connection，封装了更多的方法)
        session = SessionFactory.openSession();
        //获取接口的动态代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);

        Map<String ,Object> map = new HashMap();

    }

}
